//
// Created by Semir on 2020/4/14.
//

#include <iostream>

using namespace std;

const int MAX_T = 100005;
const int MAX_M = 10005;
int t, m;
int tim[MAX_M];
int value[MAX_M];
int dp[MAX_T];

int main() {
    cin >> t >> m;
    for (int i = 1; i <= m; i++) {
        cin >> tim[i] >> value[i];
    }
    for (int i = 1; i <= m; i++) {
        for (int j = tim[i]; j <= t; j++) {
            dp[j] = max(dp[j], dp[j - tim[i]] + value[i]);
        }
    }
    cout << dp[t] << endl;
    return 0;
}
